﻿using System;
using Code;
using System.Data;
using System.Data.SqlClient;


namespace E2_WEBSITE.Code.Admin
{
    public class CompanyDataAccess
    {
        public CompanyInfo GetCompanyInfo()
        {
            try
            {
                DataProvider provider = DataProvider.GlobalDataProvider;
                string sql = "SELECT i.[id] ID" +
                                  ", i.[CongTy] CongTy" +
                                  ", i.[Email] Email" +
                                  ", i.[SDT] DienThoai" +
                                  ", i.[DiaChi] DiaChi" +
                                  ", i.[NgayCapNhat] NgayCapNhat" +
                                  ", u.[Ten] NguoiCapNhat" +
                                  ", i.[BannerUrl] Banner " +
                              "FROM [COMPUTERSHOPDB].[dbo].[THONG_TIN] i, [COMPUTERSHOPDB].[dbo].[NGUOI_DUNG] u " +
                              "WHERE i.[NguoiCapNhat] = u.[id]";
                DataTable data = provider.ExecuteQueryDataTable(sql);
                if (data.Rows.Count > 0)
                {
                    return CompanyInfo.Load(data);
                }
                else
                {
                    return null;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public bool UpdateCompanyInfo(CompanyInfo info)
        {
            try
            {
                DataProvider provider = DataProvider.GlobalDataProvider;
                string sql = string.Format("UPDATE [COMPUTERSHOPDB].[dbo].[THONG_TIN] " +
                               "SET [CongTy] = N'{0}'" +
                                  ", [Email] = N'{1}'" +
                                  ", [SDT] = N'{2}'" +
                                  ", [DiaChi] = N'{3}'" +
                                  ", [NgayCapNhat] = N'{4}'" +
                                  ", [NguoiCapNhat] = N'{5}'" +
                                  ", [BannerUrl] = N'{6}' " +
                             "WHERE [id] = 1",
                             info.Name,
                             info.Email,
                             info.Phone,
                             info.Address,
                             DateTime.Now.ToString("M/d/yyyy hh:mm:ss tt"),
                             info.UserUpdate,
                             info.Banner);
                return (provider.ExecuteNonQuery(sql, true) > 0);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public bool UpdateCompanyInfo(string name, string email, string phone, string address, string banner)
        {
            try
            {
                DataProvider provider = DataProvider.GlobalDataProvider;
                string sql = string.Format("UPDATE [COMPUTERSHOPDB].[dbo].[THONG_TIN] " +
                               "SET [CongTy] = N'{0}'" +
                                  ", [Email] = N'{1}'" +
                                  ", [SDT] = N'{2}'" +
                                  ", [DiaChi] = N'{3}'" +
                                  ", [NgayCapNhat] = N'{4}'" +
                                  ", [BannerUrl] = N'{5}' " +
                             "WHERE [id] = 1",
                             name,
                             email,
                             phone,
                             address,
                             DateTime.Now.ToString(Utilities.DateTimeFormatString),
                             banner);
                return (provider.ExecuteNonQuery(sql, true) > 0);
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}